home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / prefs.js < prev    next >
Text File  |  2007-11-11  |  5KB  |  255 lines

  1. /*
  2.     prefs.js
  3.  
  4.     Copyright ┬⌐ 2005, 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. /* Observes extension preferences */
  8. var wot_prefs =
  9. {
  10.     init: function()
  11.     {
  12.         try {
  13.             if (this.pref) {
  14.                 return;
  15.             }
  16.  
  17.             this.ps = Components.classes["@mozilla.org/preferences-service;1"].
  18.                             getService(Components.interfaces.nsIPrefService);
  19.  
  20.             this.pref = this.ps.getBranch(null);
  21.             this.pref_default = this.ps.getDefaultBranch(null);
  22.  
  23.             /* Default values */
  24.             for (var i = 0; i < wot_prefs_bool.length; ++i) {
  25.                 this.setDefaultBool(wot_prefs_bool[i][0],
  26.                     wot_prefs_bool[i][1]);
  27.                 this[wot_prefs_bool[i][0]] = wot_prefs_bool[i][1];
  28.             }
  29.  
  30.             for (var i = 0; i < wot_prefs_char.length; ++i) {
  31.                 this.setDefaultChar(wot_prefs_char[i][0],
  32.                     wot_prefs_char[i][1]);
  33.                 this[wot_prefs_char[i][0]] = wot_prefs_char[i][1];
  34.             }
  35.  
  36.             for (var i = 0; i < wot_prefs_int.length; ++i) {
  37.                 this.setDefaultInt(wot_prefs_int[i][0],
  38.                     wot_prefs_int[i][1]);
  39.                 this[wot_prefs_int[i][0]] = wot_prefs_int[i][1];
  40.             }
  41.  
  42.             /* Add observer */
  43.             this.pbi = this.pref.QueryInterface(
  44.                             Components.interfaces.nsIPrefBranch2);
  45.             this.pbi.addObserver(WOT_PREF, this, false);
  46.  
  47.             this.updateui = false;
  48.             this.sync();
  49.  
  50.             window.addEventListener("unload", function(e) {
  51.                     wot_prefs.unload();
  52.                 }, false);
  53.         } catch (e) {
  54.             dump("wot_prefs.add: failed with " + e + "\n");
  55.         }
  56.     },
  57.  
  58.     unload: function()
  59.     {
  60.         try {
  61.             if (this.pbi) {
  62.                 this.pbi.removeObserver(WOT_PREF, this);
  63.                 this.pbi = null;
  64.             }
  65.             this.pref_default = null;
  66.             this.pref = null;
  67.             this.ps = null;
  68.         } catch (e) {
  69.             dump("wot_prefs.unload: failed with " + e + "\n");
  70.         }
  71.     },
  72.  
  73.     setupdateui: function()
  74.     {
  75.         try {
  76.             this.updateui = true;
  77.             this.sync();
  78.         } catch (e) {
  79.             dump("wot_prefs.setupdateui: failed with " + e + "\n");
  80.         }
  81.     },
  82.  
  83.     getBool: function(name, default_value)
  84.     {
  85.         try {
  86.             if (this.pref.getPrefType(WOT_PREF +
  87.                     name) == this.pref.PREF_BOOL) {
  88.                 return this.pref.getBoolPref(WOT_PREF + name);
  89.             }
  90.         } catch (e) {
  91.             dump("wot_prefs.getBool(" + name + "): failed with " + e + "\n");
  92.         }
  93.         return default_value;
  94.     },
  95.  
  96.     setBool: function(name, value)
  97.     {
  98.         try {
  99.             this.pref.setBoolPref(WOT_PREF + name, value);
  100.             return true;
  101.         } catch (e) {
  102.             dump("wot_prefs.setBool(" + name + "): failed with " + e + "\n");
  103.         }
  104.         return false;
  105.     },
  106.  
  107.     setDefaultBool: function(name, value)
  108.     {
  109.         try {
  110.             this.pref_default.setBoolPref(WOT_PREF + name, value);
  111.             return true;
  112.         } catch (e) {
  113.             dump("wot_prefs.setDefaultBool(" + name + "): failed with " +
  114.                 e + "\n");
  115.         }
  116.         return false;
  117.     },
  118.  
  119.     getInt: function(name, default_value)
  120.     {
  121.         try {
  122.             if (this.pref.getPrefType(WOT_PREF +
  123.                     name) == this.pref.PREF_INT) {
  124.                 return this.pref.getIntPref(WOT_PREF + name);
  125.             }
  126.         } catch (e) {
  127.             dump("wot_prefs.getInt(" + name + "): failed with " + e + "\n");
  128.         }
  129.         return default_value;
  130.     },
  131.  
  132.     setInt: function(name, value)
  133.     {
  134.         try {
  135.             this.pref.setIntPref(WOT_PREF + name, value);
  136.             return true;
  137.         } catch (e) {
  138.             dump("wot_prefs.setInt(" + name + "): failed with " + e + "\n");
  139.         }
  140.         return false;
  141.     },
  142.  
  143.     setDefaultInt: function(name, value)
  144.     {
  145.         try {
  146.             this.pref_default.setIntPref(WOT_PREF + name, value);
  147.             return true;
  148.         } catch (e) {
  149.             dump("wot_prefs.setDefaultInt(" + name + "): failed with " +
  150.                 e + "\n");
  151.         }
  152.         return false;
  153.     },
  154.  
  155.     getChar: function(name, default_value)
  156.     {
  157.         try {
  158.             if (this.pref.getPrefType(WOT_PREF +
  159.                     name) == this.pref.PREF_STRING) {
  160.                 return this.pref.getCharPref(WOT_PREF + name);
  161.             }
  162.         } catch (e) {
  163.             dump("wot_prefs.getChar(" + name + "): failed with " + e + "\n");
  164.         }
  165.         return default_value;
  166.     },
  167.  
  168.     setChar: function(name, value)
  169.     {
  170.         try {
  171.             this.pref.setCharPref(WOT_PREF + name, value);
  172.             return true;
  173.         } catch (e) {
  174.             dump("wot_prefs.setChar(" + name + "): failed with " + e + "\n");
  175.         }
  176.         return false;
  177.     },
  178.  
  179.     setDefaultChar: function(name, value)
  180.     {
  181.         try {
  182.             this.pref_default.setCharPref(WOT_PREF + name, value);
  183.             return true;
  184.         } catch (e) {
  185.             dump("wot_prefs.setDefaultChar(" + name + "): failed with " +
  186.                 e + "\n");
  187.         }
  188.         return false;
  189.     },
  190.  
  191.     clear: function(name)
  192.     {
  193.         try {
  194.             this.pref.clearUserPref(WOT_PREF + name);
  195.         } catch (e) {
  196.             /* dump("wot_prefs.clear(" + name + "): failed with " + e + "\n"); */
  197.         }
  198.     },
  199.  
  200.     deleteBranch: function(name)
  201.     {
  202.         try {
  203.             this.pref.deleteBranch(WOT_PREF + name);
  204.         } catch (e) {
  205.             dump("wot_prefs.delete(" + name + "): failed with " + e + "\n");
  206.         }
  207.     },
  208.  
  209.     sync: function()
  210.     {
  211.         try {
  212.             var was_enabled = this.enabled;
  213.  
  214.             for (var i = 0; i < wot_prefs_bool.length; ++i) {
  215.                 this[wot_prefs_bool[i][0]] =
  216.                     this.getBool(wot_prefs_bool[i][0], wot_prefs_bool[i][1]);
  217.             }
  218.  
  219.             for (var i = 0; i < wot_prefs_char.length; ++i) {
  220.                 this[wot_prefs_char[i][0]] =
  221.                     this.getChar(wot_prefs_char[i][0], wot_prefs_char[i][1]);
  222.             }
  223.  
  224.             for (var i = 0; i < wot_prefs_int.length; ++i) {
  225.                 this[wot_prefs_int[i][0]] =
  226.                     this.getInt(wot_prefs_int[i][0], wot_prefs_int[i][1]);
  227.             }
  228.  
  229.             /* Do stuff */
  230.             if (this.updateui) {
  231.                 wot_ui.show_elements();
  232.  
  233.                 if (was_enabled != this.enabled) {
  234.                     wot_core.update();
  235.                 }
  236.             }
  237.         } catch (e) {
  238.             dump("wot_prefs.sync: failed with " + e + "\n");
  239.         }
  240.     },
  241.  
  242.     observe: function(subject, topic, state)
  243.     {
  244.         try {
  245.             if (topic == "nsPref:changed") {
  246.                 this.sync();
  247.             }
  248.         } catch (e) {
  249.             dump("wot_prefs.observe: failed with " + e + "\n");
  250.         }
  251.     }
  252. };
  253.  
  254. wot_prefs.init();
  255.